import { defineStore } from 'pinia';
import { ref } from 'vue';

// 有效时长 毫秒*秒*分钟*小时
const ExpirationTime = 1000 * 60 * 60 * 2;

interface PlainUserInfo {
  id: number;
  username: string;
  nickname: string;
  avatar: string;
  userType: number;
  unreadMessages: number;
  token: string | null;
  expiration: number;
}

export const useUserStore = defineStore('userInfo', () => {

  // 默认用户信息
  const defaultUser: PlainUserInfo = {
    id: 0,
    username: '',
    nickname: '',
    avatar: '',
    userType: 0,
    unreadMessages: 0,
    token: null,
    expiration: 0
  }
  const userInfo = ref<PlainUserInfo>(defaultUser);
  const isLogin = ref(true);   // 默认登录状态，false表示未登录，true表示已登录

  // 页面加载时从 LocalStorage 获取用户信息
  const loadUserInfo = () => {
    const storageUserInfo = localStorage.getItem('userInfo');
    // console.log('be invoked');

    if (storageUserInfo) {
      // 如果localStorage有数据，再检查token是否为空
      const storeUserInfo = JSON.parse(storageUserInfo);
      const currentTime = new Date().getTime();
      
      if (storeUserInfo.userInfo.expiration > 0 && storeUserInfo.userInfo.expiration < currentTime) {
        // 如果expiration比当前时间小，则认为token过期，则执行登出方法
        logout();
      } else {
        if (storeUserInfo.userInfo.token) {
          // 如果token字段不为空，则更新userStore中的数据，并令isLogin为true
          userInfo.value.id = storeUserInfo.userInfo.id;
          userInfo.value.username = storeUserInfo.userInfo.username;
          userInfo.value.nickname = storeUserInfo.userInfo.nickname;
          userInfo.value.avatar = storeUserInfo.userInfo.avatar;
          userInfo.value.token = storeUserInfo.userInfo.token;
          isLogin.value = true;
        } else {
          // console.log('token is null');
          isLogin.value = false; // 如果token为空则不显示数据
        }
      }
    } else {
      // console.log('localStorage is null');
      isLogin.value = false; // 如果localStorage为空也不显示数据
    }
  };

  // 设置用户信息
  const setId = (id: number) => {
    userInfo.value.id = id
  }

  const setUsername = (username: string) => {
    userInfo.value.username = username
  }

  const setNickname = (nickname: string) => {
    userInfo.value.nickname = nickname
  }
  const setAvatar = (avatar: string) => {
    userInfo.value.avatar = avatar
  }

  const setUserType = (userType: number) => {
    userInfo.value.userType = userType
  }

  const setUnreadMessages = (unreadMessages: number) => {
    userInfo.value.unreadMessages = unreadMessages
  }

  const setToken = (token: string) => {
    userInfo.value.token = token
  }

  const setExpiration = () => {
    // 获取系统当前时间戳，加上有效时长，得到到期时间的时间戳
    const currentTimeStamp = new Date().getTime();
    userInfo.value.expiration = currentTimeStamp + ExpirationTime;
  }

  // 登出，将用户信息置空
  const logout = () => {
    isLogin.value = false
    userInfo.value.id = 0
    userInfo.value.nickname = ''
    userInfo.value.avatar = ''
    userInfo.value.unreadMessages = 0
    userInfo.value.token = null
    userInfo.value.expiration = 0
    window.location.reload();
  }

  return {
    loadUserInfo,
    isLogin,
    userInfo,
    setId,
    setUsername,
    setNickname,
    setAvatar,
    setUserType,
    setUnreadMessages,
    setToken,
    setExpiration,
    logout
  }
}, {
  persist: {
    storage: localStorage,
    key: 'userInfo',
    pick: ['userInfo']
  }

})
